package com.itic.appbase.framework.utils;

import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

public class WxUtils {

	/**
	 * 生成公众号二维码
	 * 
	 * @return
	 */
	public static Map<String, String> getTicket(String access_token, String appId, String appSecret) {
		Map<String, String> map = new HashMap<String, String>();
		JSONObject wxResult = null;
		String ticket = null;
		String path = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + access_token + "";
		String param = "{'expire_seconds': 604800,'action_name': 'QR_SCENE',"
				+ "'action_info': {'scene': {'scene_id': 4294967295}}}";
		//规范拼接json
		WxActionVo WxActionVo = new WxActionVo();
		WxActionInfoVo WxActionInfoVo = new WxActionInfoVo();
		WxActionSecneVo WxActionSecneVo = new WxActionSecneVo();
		WxActionSecneVo.setScene_id(4294967295l);
		WxActionInfoVo.setScene(WxActionSecneVo);
		WxActionVo.setAction_info(WxActionInfoVo);
		WxActionVo.setAction_name("QR_SCENE");
		WxActionVo.setExpire_seconds(604800);
		JSONObject params = JSONObject.fromObject(WxActionVo);
		
		JSONObject jsonParam = JSONObject.fromObject(param);
		wxResult = HttpRequestUtils.httpPost(path, jsonParam);
		if (wxResult != null) {
			if (wxResult.get("ticket") == null) {
				Map<String, String> tmap = getAccessToken(appId, appSecret);
				if (tmap != null) {
					access_token = tmap.get("access_token");
				}
				path = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + access_token + "";
				wxResult = HttpRequestUtils.httpPost(path, jsonParam);
				if (wxResult != null) {
					if(wxResult.get("ticket") != null){
						ticket = wxResult.getString("ticket");
						map.put("ticket", ticket);
						map.put("access_token", access_token);
					}
				}
			}
		}
		return map;
	}

	/**
	 * 公众号获取token
	 * 
	 * @return
	 */
	public static Map<String, String> getAccessToken(String appId, String appSecret) {
		Map<String, String> map = new HashMap<String, String>();
		// Map<String, String> config =
		// getWxconfig(WXconfigTypeVo.gzh.getType());
		// if (config != null) {
		/*
		 * String appId = config.get("appId"); String appSecret =
		 * config.get("appSecret");
		 */
		String path = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret="
				+ appSecret + "";
		String wxResult = HttpRequestUtils.httpGet(path);
		JSONObject obj = JSONObject.fromObject(wxResult);
		if (obj != null) {
			if(obj.get("access_token") != null){
				String access_token = obj.getString("access_token");
				map.put("access_token", access_token);
			}
		}
		// }
		return map;
	}

	/**
	 * 根据公众号唯一标识信息获取信息
	 * 
	 * @param openId
	 * @return
	 */
	public static Map<String, String> getUnionInfo(String code,String appId,String appSecret) {
		Map<String, String> map = new HashMap<String, String>();
		Map<String, String> authMap = getauthInfo(code,appId,appSecret);
		String openId = authMap.get("openId");
		String access_token = authMap.get("access_token");
		map = getGzhinfo(openId, access_token);
		return map;
	}

	/**
	 * 获取用户信息及公众号关注情况
	 * 
	 * @param openId
	 * @param access_token
	 * @return
	 */
	public static Map<String, String> getGzhinfo(String openId, String access_token) {
		Map<String, String> map = new HashMap<String, String>();
		String path = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openId
				+ "&lang=zh_CN";
		String wxResult = HttpRequestUtils.httpGet(path);
		JSONObject obj = JSONObject.fromObject(wxResult);
		if (obj != null) {
			if(obj.get("subscribe") != null){
				map.put("subscribe", obj.getString("subscribe"));
				map.put("unionid", obj.getString("unionid"));
			}
		}
		return map;
	}

	/**
	 * 微信开放平台配置信息
	 * 
	 * @return
	 */
	/*
	 * public static Map<String, String> getWxconfig(String type) { Map<String,
	 * String> map = new HashMap<String, String>(); if
	 * (WXconfigTypeVo.kf.getType().equals(type)) { String appId =
	 * "wxabd9e33f81f1fe9a"; String appSecret =
	 * "c74d8ee0724760958b940860bf29bdb9"; map.put("appId", appId);
	 * map.put("appSecret", appSecret); } if
	 * (WXconfigTypeVo.gzh.getType().equals(type)) { String appId =
	 * "wx58c6610262564a57"; String appSecret =
	 * "9886b64c7a7f3fbcdb280c042d29fe16"; map.put("appId", appId);
	 * map.put("appSecret", appSecret); } return map; }
	 */

	/**
	 * 获取微信用户授权信息
	 * 
	 * @param code
	 * @return
	 */
	public static Map<String, String> getauthInfo(String code,String appId,String appSecret) {
		Map<String, String> returnMap = new HashMap<String, String>();
		/*Map<String, String> map = WxUtils.getWxconfig(WXconfigTypeVo.kf.getType());
		String appId = map.get("appId");
		String appSecret = map.get("appSecret");*/
		String grant_type = "authorization_code";
		String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code="
				+ code + "&grant_type=" + grant_type + "";
		String wxResult = HttpRequestUtils.httpGet(path);
		JSONObject obj = JSONObject.fromObject(wxResult);
		if (obj != null) {
			if(obj.get("openId") != null){
				String openId = obj.getString("openid");
				String accessToken = obj.getString("access_token");
				returnMap.put("openId", openId);
				returnMap.put("access_token", accessToken);
			}
		}
		return returnMap;
	}

	/**
	 * 根据code获取微信用户信息
	 * 
	 * @param code
	 * @return
	 */
	public static Map<String, String> getWXuserBycode(String code,String appId,String appSecret) {
		Map<String, String> authMap = WxUtils.getauthInfo(code,appId,appSecret);
		String openId = authMap.get("openId");
		String access_token = authMap.get("access_token");
		Map<String, String> resultMap = WxUtils.getWxUserinfo(openId, access_token);
		return resultMap;
	}

	/**
	 * 获取用户微信唯一标识openiD
	 * 
	 * @param code
	 * @return
	 */
	public static String getopenId(String code,String appId,String appSecret) {
		String openId = null;
		/*Map<String, String> map = WxUtils.getWxconfig(WXconfigTypeVo.kf.getType());
		String appId = map.get("appId");
		String appSecret = map.get("appSecret");*/
		String grant_type = "authorization_code";
		String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code="
				+ code + "&grant_type=" + grant_type + "";
		String wxResult = HttpRequestUtils.httpGet(path);
		JSONObject obj = JSONObject.fromObject(wxResult);
		if (obj != null) {
			if(obj.get("openid") != null){
				openId = obj.getString("openid");
			}
		}
		return openId;
	}

	/**
	 * 获取微信用户信息
	 * 
	 * @param openId
	 * @param accesstoken
	 * @return
	 */
	public static Map<String, String> getWxUserinfo(String openId, String accesstoken) {
		Map<String, String> map = new HashMap<String, String>();
		String lang = "zh_CN";
		String path = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accesstoken + "&openid=" + openId
				+ "&lang=" + lang + "";
		String wxResult = HttpRequestUtils.httpGet(path);
		JSONObject obj = JSONObject.fromObject(wxResult);
		if (obj != null) {
			if(obj.get("unionid") != null){
				String nickname = obj.getString("nickname");
				String headimgurl = obj.getString("headimgurl");
				String unionId = obj.getString("unionid");
				String sex = obj.getString("sex");
				String province = obj.getString("province");
				String city = obj.getString("city");
				String country = obj.getString("country");
				map.put("nickname", nickname);
				map.put("headimgurl", headimgurl);
				map.put("openId", openId);
				map.put("unionId", unionId);
				map.put("sex", sex);
				map.put("province", province);
				map.put("city", city);
				map.put("country", country);
			}
		}
		return map;
	}
}
